Okay, so in today's lesson we are going to be looking at the second type of loop which are for loops.
Okay.
So let's add a script into a part.
Okay.
And create a variable for that part.
It's equal to script dot parent.
Okay.
So if loops are again quite they're quite similar to while loops okay.
Um they repeat code as fast as it can basically.
The difference though is that for loops don't use a condition.
Instead you tell it how many times you want it to repeat the code.
So you could say, I want to run this code 50 times, and that would be something you could do using
a for loop.
So if you're wanting code to repeat, um, based on a condition, you're going to want to be using,
the previous one we learned, which is while loops.
But if you wanted to repeat a select amount of times, you can use a for loop.
So for loops start with the for keyword.
Okay.
Um and then we have our condition space.
So it's still kind of like a condition.
But the condition isn't a property.
It's um, it's getting to a, it's a how many times it's running instead.
Okay.
Um, now, unfortunately, it's not as simple.
Just saying for ten times do.
This doesn't quite work like that.
Okay.
Instead, um, it's a bit difficult.
Um, it's a bit of a difficult concept to understand, but, um, how it works is you have some kind
of variable which stores a number.
Okay.
So this could be number is equal to.
This could be count is equal to or it could be I is equal to.
Okay.
I is typically used for this um for reasons through of tables and stuff that you'll learn later on in
the course but is typically used for that.
Okay.
Um, and the difficult bit that might seem a bit confusing to start off with is you actually give I
three numbers.
Okay.
Um, and so this could look something like this, for example, that would be the correct format to
do a for loop.
So the reason why you give it three numbers is because those three numbers represent different things.
So I'm going to write this out in notes here.
So what basically it looks like is the three numbers.
The first one is the starting number.
The second is the ending number and the third is the index number.
Okay.
So that's kind of what it looks like.
So what actually is happening here is that the first time this code runs we have a variable called I
which stores a number.
And the number that it is when it first starts is one.
That's our starting number.
The starting number is the first number we give it.
So the start off with I is equal to one.
It will then run.
It will then run our code while the I variable is equal to is equal to one.
Then when it goes to one it the second time it adds the index number onto the starting number.
So the third number is added onto the first number.
So in this case we are adding one onto one.
So IBM becomes two and it runs our code when I is equal to two.
It then repeats it again.
It adds one onto one onto one onto I.
So IBM becomes three.
It runs it when I is three runs.
It's when when I is four it's five, it's six.
So on.
And then when it get up when it gets up to ten it runs.
It's when I is equal to ten.
But then what happens when I is equal to ten.
You can't add.
If you added the index number onto the starting number I would then be equal to 11.
And if I if I is equal to 11 it's bigger than the ending number.
The number we wanted it to stop at.
So it stops running in a for loop.
Okay.
Um, what might what might make this a bit easier to see is if we just print, um, the variable I.
Okay, so because I is a variable and not a string, we don't need to use these speech marks.
We could use those if we wanted to print.
Um, the, um, yeah, we could use the speech marks if we wanted to print the letter I.
But we're wanting to print.
I is equal to.
So you don't include the speech marks only include speech marks when you're wanting to print a string.
So actual text we're not wanting to print text.
In this case we're wanting to print what is okay.
And let's maybe wait one second as well so it doesn't real time.
Let's run the game and see what happens.
So at first ones I is equal to one, then two, then three, then four, then five, and so on.
So each time it's adding one onto I.
Until it reaches ten and then it stops.
Okay, so it is a bit of a difficult concept to understand because from from looking at the first time,
you might think, you might think, you know, why are we giving i3 numbers?
But hopefully you understand that really we're only giving it one number and then we're also telling
it when, when it's went and and what add on each time.
Okay.
Um, and the reason you might want to use a for loop is quite similar to the example we used in previous
in the previous lesson, which is when you want to constantly add or take away a value from a property.
You wouldn't necessarily use this to assign a property to the same value ten times.
Like you wouldn't say make.
Make the parts anchor ten times because you only need to do it once and then it's finished.
So it's more so used to assign properties, increasing or decreasing values, or timesing and dividing
values, for example.
Okay.
Um, so let's have a go at using this to make the size of this part increase okay.
So let's say part dot size we know size is a vector three value.
So we can say vector three dot new.
Then we need to write in our three numbers here.
Now, what I'd like to happen here is the part starts off as this little cube here, and it slowly gets
bigger and bigger and bigger.
So to do this we can actually use the I variable in our code.
So the biggest advantage of using a for loop is that you have a variable called I or whatever you decide
to call it, which has a number which increases each time.
You can use.
So what I can do is I can save a size is equal to I comma, I comma, I okay.
So just like how our variables we see it as I but I is actually stored to whatever it is, whatever
it calculates based on the starting number and the index number.
So if it's a second time running in this case I will be equal to two and so on.
Okay.
Um so what will happen here.
First time it runs it's I is equal to the starting number which is one.
So the size is equal to one comma, one comma one.
If it runs it again and it adds the index number onto the starting number.
So I is equal to two.
So the size is equal to 2 to 2.
And if you put a weight in there maybe one second we can see the part increasing in size.
It continues to be getting bigger.
And here it reaches to ten and then it will stop and you can see a bit of it is under the base plate.
But then it gets to ten and it stops.
Okay.
If you wanted to, maybe we can make it go a bit larger.
Maybe we can make it go to 50 and make it wait less time.
See how that might work.
They increasing their.
Until it reaches 50 and it will stop soon.
Yeah, there we go.
And I don't know, maybe you wanted to start it off at a greater size.
Maybe you wanted to start at a size of ten.
Well, then change the starting number to ten.
So when the first time it runs, I is equal to ten, then add the one at I is equal to 11, and so on.
Try out then has a bit of a head start and then it continued going from there.
Okay.
And maybe if you wanted to grow faster you could increase the size of your index number let's say to
ten for example.
So it is adding a, it's adding a greater value onto I.
So it's so it's more jumpy.
If we play it we can see it here.
It jumps size quite big quite quickly like that there okay.
So for loops is is probably the out of the three types of loop is probably the one that people find
the most confusing and take the most time to understand.
I believe it's probably because we're giving I this start bit here that confuses most people because
I just acts like a variable we know.
Imagine imagining we're saying like um, local I at the start it's just like that, but without the
local bit.
Um, so as long as you understand that the first number is the starting number, the second number is
the ending number, and the third number is your index number or your counting number.
It's the number that you increase the starting number by each time.
Then you should be okay with for loops.
Okay.
Um so have a try with those.
And next lesson we will finish off um looking at loops by looking at repeat loops.